package demo1;

import demo1.MyStack;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

public class Test {
    public static void main3(String[] args) {
        //链栈
        LinkedList<Integer> stack = new LinkedList<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        System.out.println(stack.pop());
        System.out.println(stack.peek());
    }

    //有效的括号
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (ch == '(' || ch == '{' || ch == '[') {
                //左括号入栈
                stack.push(ch);
            } else {
                //遇到右括号
                if(stack.empty()) {
                    //栈空，字符串不空，不匹配
                    return false;
                }
                char ch2 = stack.pop();//弹出左括号与右括号比较
                if(ch2 == '(' && ch == ')' || ch2 == '{' && ch == '}'
                        || ch2 == '[' && ch == ']') {
                    //左括号与右括号匹配
                } else {
                    return false;
                }
            }
        }
        if(stack.empty()) {
            //字符串遍历完了，栈也空，说明都匹配了
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        MyStack1 stack = new MyStack1();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.peek());
        System.out.println(stack.peek());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
    }

    public static void main1(String[] args) {
        MyStack stack = new MyStack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.peek());
        System.out.println(stack.peek());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
    }
}
